package com.lanou.myapp.comic.recommendedcomic;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.circlepicture.MyCircle;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.lanou.myapp.circlepicture.Circlelmage;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.lanou.myapp.R.id.iv_cover_recommended_comic;

/**
 * Created by dllo on 16/1/14.
 */
public class RecyclerRecommendedAdapter extends RecyclerView.Adapter<RecyclerRecommendedAdapter.MyViewHolder>{
    private Context context;
    private ImageLoader imageLoader;
//    private ImageLoader.ImageCache imageCache;
//    private LruCache<String, Bitmap> lruCache;
//    private RequestQueue queue;
    private List<ComicsBeans.DataEntity.ComicsEntity> data;
    private MyOnclick myOnclick;


    public void setMyOnclick(MyOnclick myOnclick){
        this.myOnclick = myOnclick;
    }


    public RecyclerRecommendedAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance()._getImageLoader();

    }

    public void addData(List<ComicsBeans.DataEntity.ComicsEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recommended_comic, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
//        holder.networkImageView.setDefaultImageResId(R.mipmap.v6_boy_unseletor_icon);
//        holder.networkImageView.setErrorImageResId(R.mipmap.v5_icon_usercenter);
//        holder.networkImageView.setImageUrl(data.get(position).getCover_image_url(), imageLoader);


        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.imageView, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
        VolleySingleton.getImageLoader().get(data.get(position).getCover_image_url(), listener,400,300);

//        Glide.with(context).load(data.get(position).getCover_image_url()).into(holder.imageView);


//        Glide.with(context).load(data.get(position).getTopic().getUser().getAvatar_url()).into(holder.image_circle);

        ImageLoader.ImageListener listenerCircle = ImageLoader.getImageListener(holder.image_circle, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
        VolleySingleton.getImageLoader().get(data.get(position).getTopic().getUser().getAvatar_url(), listenerCircle,200,200);

        holder.tv_name.setText(data.get(position).getTopic().getTitle());
        holder.tv_title.setText(data.get(position).getTitle());
        holder.tv_author.setText(data.get(position).getTopic().getUser().getNickname());
        holder.tv_like.setText(String.valueOf(data.get(position).getLikes_count()));
        holder.tv_comment.setText(String.valueOf(data.get(position).getComments_count()));

        if(myOnclick != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    myOnclick.MyonClick(pos);
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size()/2;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView networkImageView;
        private ImageView imageView;
        private CircleImageView image_circle;
        private TextView tv_name, tv_title, tv_author, tv_like, tv_comment;

        public MyViewHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.iv_cover_recommended_comic);
            image_circle= (CircleImageView) itemView.findViewById(R.id.iv_author_recommended_comic);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name_recommended_comic);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title_recommended_comic);
            tv_author = (TextView) itemView.findViewById(R.id.tv_author_recommended_comic);
            tv_like = (TextView) itemView.findViewById(R.id.tv_num_recommended_comic);
            tv_comment = (TextView) itemView.findViewById(R.id.tv_information_recommended_comic);
        }
    }

}
